package com.study.demo.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/**
 * @author: elvin
 * @time: 2019-05-08 16:25
 * @desc:
 **/
@Controller
@RequestMapping("book")
@SessionAttributes(value = {"book", "desc"})
public class BookController {

    @GetMapping("index")
    public String index(Model model){
        model.addAttribute("book", "go left or go right");
        model.addAttribute("desc", "this is a nice book !");
        return "redirect:get";
    }

    @GetMapping("get")
    public String get(@ModelAttribute("book") String book, ModelMap model, SessionStatus sessionStatus){
        System.out.println("------get------");
        System.out.println("get by @ModelAttribute: " + book);
        System.out.println("get by ModelMap: "+ model.get("book") + " - " + model.get("desc"));
        sessionStatus.setComplete();
        System.out.println("set @ModelAttribute empty");
        return "redirect:complete";
    }

    @GetMapping("complete")
    public String complete(ModelMap model){
        System.out.println("------complete------");
        System.out.println(model.get("book") + " - " + model.get("desc"));
        return "book";
    }
}
